package com.wyj.utils

import android.content.Context
import android.text.InputType
import com.hjq.toast.Toaster
import com.qmuiteam.qmui.util.QMUIKeyboardHelper
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable
import java.util.concurrent.TimeUnit

object QMUIDialogHelper {

    @JvmStatic
    @JvmOverloads
    fun show(context: Context, msg: String, icon: Int = QMUITipDialog.Builder.ICON_TYPE_NOTHING, delay: Long = 2000) {
        val dialog = QMUITipDialog.Builder(context).setIconType(icon).setTipWord(msg).create();
        dialog.show();
        Observable.timer(delay, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe {
            dialog.dismiss()
        }
    }

    @JvmStatic
    fun createLoadingDialog(context: Context): QMUITipDialog {
        val dialog = QMUITipDialog.Builder(context).setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING).create()
        dialog.setCanceledOnTouchOutside(false)
        dialog.setCancelable(false)
        return dialog
    }

    @JvmStatic
    @JvmOverloads
    fun createTipDialog(context: Context, title: String = "", msg: String, okClick: QMUIDialogAction.ActionListener? = null): QMUIDialog {
        return QMUIDialog.MessageDialogBuilder(context).setTitle(title).setMessage(msg).addAction("确定") { dialog, index ->
            dialog.dismiss()
            okClick?.onClick(dialog, index)
        }.create()
    }


    @JvmStatic
    @JvmOverloads
    fun createMessageDialog(context: Context, title: String = "提示", msg: String, okClick: QMUIDialogAction.ActionListener, cancelClick: QMUIDialogAction.ActionListener? = null): QMUIDialog {
        return QMUIDialog.MessageDialogBuilder(context).setTitle(title).setMessage(msg).addAction("取消") { dialog, index ->
            dialog.dismiss()
            cancelClick?.onClick(dialog, index)
        }.addAction("确定", okClick).create()
    }

    @JvmStatic
    fun showDeleteDialog(context: Context, okClick: QMUIDialogAction.ActionListener) {
        createMessageDialog(context, msg = "确认删除？", okClick = QMUIDialogAction.ActionListener { dialog, index ->
             dialog.dismiss()
            okClick.onClick(dialog, index)
        }).showWithImmersiveCheck()
    }

    interface OnEditListener {
        fun onEditListener(text: String)
    }


    @JvmStatic
    @JvmOverloads
    fun createEditDialog(context: Context, title: String = "", inputType: Int = InputType.TYPE_CLASS_TEXT, placeholder: String, defaultText: String = "", canEmpty: Boolean = false, onEditListener: OnEditListener, cancelClick: QMUIDialogAction.ActionListener? = null): QMUIDialog {
        var builder: QMUIDialog.EditTextDialogBuilder? = null
        builder = QMUIDialog.EditTextDialogBuilder(context).setTitle(title).setInputType(inputType)
                .setDefaultText(defaultText).setPlaceholder(placeholder).addAction("取消") { dialog, index ->
                    builder?.editText?.let {
                        QMUIKeyboardHelper.hideKeyboard(it)
                    }
                    dialog.dismiss()
                    cancelClick?.onClick(dialog, index)
                }.addAction("确定") { dialog, _ ->
                    val text = builder?.editText?.text?.toString() ?: ""
                    if (!canEmpty && isNullOrEmpty(text)) {
                        Toaster.show(if (Utils.isNullOrEmpty(placeholder)) "请输入内容" else placeholder)
                    } else {
                        builder?.editText?.let {
                            QMUIKeyboardHelper.hideKeyboard(it)
                        }
                        dialog.dismiss()
                        onEditListener.onEditListener(text)
                    }
                }
        return builder.create()
    }
}